package com.jarvis.zeng.design.pattern.filter.datapermission;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Component
public class MessageListener {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring.xml");
        MessageListener listener = applicationContext.getBean(MessageListener.class);
        Message msg = new Message();
        msg.setHour(10);
        msg.setName("jarvis");
        msg.setType(15);
        listener.listen(msg);
    }

    public void listen(Message message){
        boolean isFilter = doFilter(message);
        if(isFilter){
            System.out.println("已过滤");
        }else{
            System.out.println("过滤不满足条件");
        }
    }

    public boolean doFilter(Message message){
        List<IMessageFilter> filters = SpringUtils.getBeansOfType(IMessageFilter.class);
        Collections.sort(filters);
        for(IMessageFilter filter : filters)
        {
            if(!filter.doFilter(message))
            {
                return false;
            }
        }
        return true;
    }
}
